import java.awt.*;
import java.util.List;

public class Missile {
    public static final int XSPEED = 10;
    public static final int YSPEED = 10;
    public static final int WIDTH = 10;
    public static final int HEIGHT = 10;
    
    private boolean good;
    private int x, y;
    private boolean live = true;
    Tank.Direction dir;
    TankClient tc;
    
    public boolean isLive() { return live; }
    public void setLive(boolean live) { this.live = live; }
    
    // Konstruktor utama untuk missile
    public Missile(int x, int y, Tank.Direction dir, TankClient tc, boolean good) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.tc = tc;
        this.good = good;
    }
    
    public void draw(Graphics g) {
        if (!live) {
            tc.missiles.remove(this);
            return;
        }
        
        Color c = g.getColor();
        g.setColor(Color.BLACK);
        g.fillOval(x, y, WIDTH, HEIGHT);
        g.setColor(c);
        move();
    }
    
    // Menghasilkan boundary rectangle untuk collision detection
    public Rectangle getRect() {
        return new Rectangle(x, y, WIDTH, HEIGHT);
    }
    
    /**
     * Cek collision dengan tank-tank
     */
    public boolean hitTanks(List<Tank> tanks) {
        for (Tank t : tanks) {
            if (hitTank(t)) {
                return true;
            }
        }
        return false;
    }
    
    /**
     * Cek collision dengan satu tank
     */
    public boolean hitTank(Tank t) {
        if (live && this.getRect().intersects(t.getRect()) 
            && t.isLive() && (this.good != t.isGood())) {
            if (t.isGood()) {
                t.setLife(t.getLife() - 20); // berikan damage lebih besar
                if (t.getLife() <= 0) {
                    t.setLive(false);
                }
            } else {
                t.setLive(false);
            }
            
            this.live = false;
            // Tambahkan efek explosion
            Explode e = new Explode(t.getX(), t.getY(), tc);
            tc.explodes.add(e);
            
            
            return true;
        }
        return false;
    }
    
    /**
     * Cek collision dengan dinding
     */
    public boolean hitWall(Wall w) {
        if (live && this.getRect().intersects(w.getRect())) {
            live = false;
            return true;
        }
        return false;
    }
    
    /**
     * Perpindahan posisi peluru sesuai arah
     */
    private void move() {
        switch (dir) {
            case L:  x -= XSPEED; break;
            case LU: x -= XSPEED; y -= YSPEED; break;
            case U:  y -= YSPEED; break;
            case RU: x += XSPEED; y -= YSPEED; break;
            case R:  x += XSPEED; break;
            case RD: x += XSPEED; y += YSPEED; break;
            case D:  y += YSPEED; break;
            case LD: x -= XSPEED; y += YSPEED; break;
        }
        // Jika missile keluar area, maka set tidak aktif
        if (x < 0 || y < 0 || x > TankClient.GAME_WIDTH || y > TankClient.GAME_HEIGHT) {
            live = false;
        }
    }
}